﻿guestBookApp.controller("indexController",
    ["$scope", "$location", "$modal", "DataService", 
    function ($scope, $location, $modal, DataService) {
        $scope.data = {};
        $scope.currentPage = 1;
        $scope.pageSize = 25;
        $scope.predicate = 'createdAt';
        $scope.reverse = true;

        $scope.pageChangeHandler = function (num) {
        };
        DataService.getRecords().then(function (results) {
                // on success            
                $scope.data.records = results.data;
                $scope.totalItems = $scope.data.records.length;
            },
            function (results) {
                if (results.statusText.length > 0) {
                    $scope.hasFormError = true;
                    $scope.formErrors = results.statusText;
                }
            });

        $scope.showAddNewRecordForm = function () {
            $modal.open({
                templateUrl: 'Pages/recordForm.html',
                controller: 'recordFormController'
            }).result.then(function (result) {
                $scope.predicate = 'createdAt';
                $scope.reverse = true;
                $scope.currentPage = 1;
                $scope.data.records.unshift(result);                
            }, function (result) {
                if (results.statusText.length > 0) {
                    $scope.hasFormError = true;
                    $scope.formErrors = results.statusText;
                }
            });
        };
    }]);